700. 二叉搜索树中的搜索
为保证权益,题目请参考 700. 二叉搜索树中的搜索(From LeetCode).
解决方案1
Python
python
# 700. 二叉搜索树中的搜索
# https://leetcode-cn.com/problems/search-in-a-binary-search-tree/
################################################################################
# Definition for a binary tree node.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def searchBST(self, root: TreeNode, val: int) -> TreeNode:
if root is None:
return None
if root.val == val:
return root
else:
if root.val < val:
return self.searchBST(root.right, val)
else:
return self.searchBST(root.left, val)
################################################################################
if __name__ == "__main__":
solution = Solution()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31